package cn.com.ddapp.ddm.widget.pulltorefresh.pullableview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Created by Administrator on 2016/8/15 0015.
 */
public class PullableRecyclerview extends RecyclerView implements Pullable {

//	LinearLayoutManager lm;

	public PullableRecyclerview(Context context) {
		super(context);
	}

	public PullableRecyclerview(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
	}

	public PullableRecyclerview(Context context, @Nullable AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean canPullDown() {
		if (getAdapter().getItemCount() == 0) {
			// 没有item的时候也可以下拉刷新
			return true;
		} else if (
				((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition() == 0 && getChildAt(0).getTop() >= 0
				) {
			// 滑到RecyclerView的顶部了
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean canPullUp() {
		if (getAdapter().getItemCount() == 0) {
			// 没有item的时候也可以上拉加载
			return true;
		} else if (((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition() == (getAdapter().getItemCount() - 1)) {
			// 滑到底部了
			if (getChildAt(((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition() - ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition()) != null
					&& getChildAt(
					((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition() - ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition()).getBottom() <= getMeasuredHeight())
				return true;
		}
		return false;
	}
}
